package com.lagou.rpc.consumer.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class ServerConfig {
    private static List<InetSocketAddress> rpcServerAddress = new ArrayList<>();

    @Value("${rpc.server.address}")
    public void setRpcServerAddress(String address) {

//        System.out.println(address);
        String[] addresses = address.split(",");
        for (String s : addresses) {
//            System.out.println(s);
            String[] ipport = s.split(":");
            rpcServerAddress.add(new InetSocketAddress(ipport[0], Integer.parseInt(ipport[1])));
        }

        for (InetSocketAddress serverAddress : rpcServerAddress) {
//            System.out.println(serverAddress.getAddress() + " ---");
            System.out.println(serverAddress.getAddress() + ":" + serverAddress.getPort());
        }
    }

    public static List<InetSocketAddress> getRpcServerAddress() {
        return rpcServerAddress;
    }
}
